package com.wjp.dinosaur.config;

import com.wjp.dinosaur.handler.JwtInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    /**
     * 这里主要为了解决跨域问题,所以重写addCorsMappings方法
     * @param registry
     */
    public void addCorsMappings(CorsRegistry registry){
        // 设置允许跨域的路径
        registry.addMapping("/**")  // 当前站点的亲求地址都支持跨域
                //设置允许跨域请求的域名,二选一allowedOrigins或allowedOrigins
                //.allowedOrigins("*") //注意access-control-allow-origin时,allowedOrigins不能为*
                //.allowedOrigins("域名") //如果是localhost则很难配置，因为在跨域请求的时候，外部域的解析可能是localhost、127.0.0.1、主机名
                .allowedOriginPatterns("*") // 所有的外部域都可跨域访问
                // 放行那些原始域（请求方式）
                .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
                // 设置允许的请求头
                .allowedHeaders("*")
                // 暴露那些头部信息（因为跨域访问默认不能获取全部头部信息）
                .exposedHeaders("access-control-allow-headers",
                        "access-control-allow-methods",
                        "access-control-allow-origin",
                        "access-control-max-age",
                        "X-Frame-Options",
                        "token")
                // 是否允许证书（是否支持跨域用户凭证），不在默认开启
                .allowCredentials(true)
                // 跨域允许时间
                .maxAge(3600 * 24);
    }

    @Resource
    private JwtInterceptor jwtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器，要声明拦截器对象和要拦截的请求.
        registry
                .addInterceptor(jwtInterceptor)
                .addPathPatterns("/score/**"); // 所有路径都被拦截
//         ;

//                .excludePathPatterns("/login")  // 登录放行
//                .excludePathPatterns("/register")
//                .excludePathPatterns("/swagger/**")
//                .excludePathPatterns("/swagger-resources/**")
//                .excludePathPatterns("/swagger-ui.html")
//                .excludePathPatterns("/swagger-ui.html")
//                .excludePathPatterns("/swagger-ui.html")
//                .excludePathPatterns("/swagger-ui.html");
    }

    @Bean
    public CharacterEncodingFilter characterEncodingFilter(){
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);
        return filter;
    }



    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/resources/", "classpath:/static/", "classpath:/public/"
    };

    /**
     * 跨域配置后swagger2可能不能访问，需要增加如下配置
     * 原因：不能访问的原因的swagger的内置接口被拦机器拦下来，需要将swagger加到拦截器的排除列表中。
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/**").addResourceLocations(
                CLASSPATH_RESOURCE_LOCATIONS);
        registry.addResourceHandler("/img/**").addResourceLocations("file:" + "/home/ruipeng/img/");

    }

}
